/**
 *  列表页
 *
 * 作者 zhangwei
 * 日期 2024-09-03 13:16:21
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-incentiveQuotaApplyAudit'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#incentiveQuotaApplyAudit-table-form'),
        tableIns;

    laydate.render({
        elem: "#year",
        type: "year",
        trigger:'click',
        showBottom:false
    })

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'incentiveQuotaApplyAuditTable',
            url: ctx + 'incentiveQuotaApply/list/auditList',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {title: '序号',align:'center', type: 'numbers',width: 80},
                {
                    field: 'tenantId',
                    title: '公司名称',
                    width: 160,
                    hide: !hasHeadCompanyRole,
                    templet: function (d) {
                        var tenant = tenantList.filter(item => item.tenantId == d.tenantId);
                        return tenant[0].tenantName;
                    }
                },
                {field: 'quotaApplyNo',align:'center', title: '申请编号', width: 200,style:'color:#01aaed;cursor:pointer',event:'detail'},
                {field: 'year',align:'center', title: '年份', width: 80},
                {field: 'realName',align:'center', title: '申请人', width: 100},
                {field: 'createdTime',align:'center', title: '申请时间', width: 160},
                {field: 'applyQuota',align:'center', title: '申请额度', width: 140},
                {field: 'applyRemark',align:'left', title: '申请理由', minWidth: 120},
                {field: 'status',align:'center', title: '状态', width: 90,templet:function (d) {
                    let str = '';
                        switch (d.status){
                            case "1":str='<span class="layui-badge febs-bg-cyan">待审核</span>';break;
                            case "2":str='<span class="layui-badge febs-bg-cyan">审核中</span>';break;
                            case "3":str='<span class="layui-badge febs-bg-cyan">审核通过</span>';break;
                            case "4":str='<span class="layui-badge febs-bg-red">审核退回</span>';break;
                        }
                        return str;
                    }},
                {title: '操作',align:'center',  toolbar: '#incentiveQuotaApplyAudit-tool', width: 100}
            ]],
            defaultToolbar: []
        });
    }

    table.on('sort(incentiveQuotaApplyAuditTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('tool(incentiveQuotaApplyAuditTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'Audit') {
            febs.modal.open('审核', 'incentiveQuotaApply/audit/' + data.quotaApplyId, {
                area: $(window).width() <= 750 ? '90%': ['50%','95%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#incentiveQuotaApplyAudit-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        } else if(obj.event === 'detail'){
            febs.modal.open('申请详情', 'incentiveQuotaApply/detail/'+data.quotaApplyId, {
                btn: ['关闭'],
                area: $(window).width() <= 750 ? '90%': ['50%','80%'],
                shadeClose:true,
                yes: function (index, layero) {
                    layer.closeAll();
                },
                cancel:function () {
                },
            });
        }
    });

    function getQueryParams() {
        return {
            tenantId: $searchForm.find("select[name='tenantSelect']").find("option:selected").val(),
            quotaApplyNo: $searchForm.find('input[name="quotaApplyNo"]').val()?.trim(),
            status: $searchForm.find('input[name="status"]').val()?.trim(),
            year: $searchForm.find('input[name="year"]').val()?.trim(),
            realName: $searchForm.find('input[name="realName"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});